/*This file contains the ContactListFragment, 
 * with the list of the connected contacts. 
 * When you select an item from this list, the
 * ChatFragment is created/modified.
 * This file has no layout. */
package dk.aau.walkietalkieui.fragment;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import dk.aau.walkietalkie.database.DBController;
import dk.aau.walkietalkieui.IDBUpdateNotifier;
import dk.aau.walkietalkieui.IMessageNotifier;
import dk.aau.walkietalkieui.R;
import dk.aau.walkietalkieui.WalkieTalkieApplication;
import dk.aau.walkietalkieui.adapter.MenuListAdapter;
import dk.aau.walkietalkieui.message.MyMessage;

public class ContactListFragment extends ListFragment implements
		IDBUpdateNotifier, IMessageNotifier {
	private String selected_ip = "";
	private String selected_name = "";

	public static final String TAG = "ContactListFragment";
	private MenuListAdapter adapter;

	public String getSelectedIp() {
		return selected_ip;
	}

	public String getSelectedName() {
		return selected_name;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivity().setTitle(R.string.contact_list);
		getActivity().setTitleColor(getResources().getColor(R.color.white));

		// Clear the IP address table
		DBController controller = new DBController(
				WalkieTalkieApplication.getContext());
		controller.deleteTable();

		adapter = new MenuListAdapter(getActivity());
//
//		// Start the receiver and data entry alive counter threads
//		WalkieTalkieApplication.getApplication().setViewUpdater(this);
//		WalkieTalkieApplication.getApplication().getTranferReceiver()
//				.setMessageNotifier(this);
	}

	@Override
	public void onStart() {
		super.onStart();

		// Start the receiver and data entry alive counter threads
		WalkieTalkieApplication.getApplication().setViewUpdater(this);
		WalkieTalkieApplication.getApplication().getTranferReceiver()
				.setMessageNotifier(this);

		setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		if (adapter != null) {
			adapter.setSelectedItem(position);
		}

		selected_ip = adapter.getItem(position).getIp();
		selected_name = adapter.getItem(position).getName();

		try {

			((IContactListFragment) getActivity()).onMenuItemSelected(position);

		} catch (ClassCastException ce) {
			Log.e(TAG, getActivity().getString(R.string.parent_interface_error));

		} catch (Exception e) {
			Log.e(TAG, getActivity().getString(R.string.unhandled_error));
		}
	}

	public interface IContactListFragment {
		public void onMenuItemSelected(int position);
	}

	@Override
	public void UpdateUI() {
		// TODO Auto-generated method stub
		adapter.updateList();

		Handler mainHandler = new Handler(WalkieTalkieApplication.getContext()
				.getMainLooper());
		Runnable myRunnable = new Runnable() {
			@Override
			public void run() {
				adapter.notifyDataSetChanged();
			}
		};

		mainHandler.post(myRunnable);
	}

	@Override
	public void UpdateMessageView(MyMessage message) {
		for (int i = 0; i < adapter.getCount(); i++) {
			if (adapter.getItem(i).getIp().equals(message.getIPOwn())) {
				final int index = i;
				final MyMessage msg = message;
//				Handler mainHandler = new Handler(WalkieTalkieApplication
//						.getContext().getMainLooper());
//				Runnable myRunnable = new Runnable() {
//					@Override
//					public void run() {
						((IContactListFragment) getActivity())
								.onMenuItemSelected(index);
//						((ChatFragment) getFragmentManager().findFragmentByTag(
//								ChatFragment.TAG)).displayMessage(msg);
////					}
//				};
//
//				mainHandler.post(myRunnable);
				break;
			}
		}
	}
}
